﻿Imports DevExpress.Utils
Imports DevExpress.XtraEditors
Imports DevExpress.XtraEditors.Controls
Imports System
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraLayout
Imports DevExpress.Utils.Win
Imports DevExpress.XtraEditors.Registrator
Imports DevExpress.XtraEditors.Repository
Imports System.Reflection
Imports DevExpress.Skins
Imports DevExpress.XtraEditors.ViewInfo
Imports DevExpress.XtraEditors.Drawing
Imports DevExpress.XtraEditors.Popup

Namespace IControls
    Public Class ISearchLookupEdit
        Inherits SearchLookUpEdit
        Private WithEvents view As DevExpress.XtraGrid.Views.Grid.GridView
        ' Methods
        Private flag As Boolean
        Friend WithEvents GridView5 As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents fProperties As DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit
        Friend WithEvents fPropertiesView As DevExpress.XtraGrid.Views.Grid.GridView
        Private flagEnter As Boolean
        Private flagPaint As Boolean
        Private rows As DataRow
        Private bd As BindingSource
        Private colFilter As String
        Private txt As TextEdit
        Friend WithEvents GridView1 As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents GridView2 As DevExpress.XtraGrid.Views.Grid.GridView
        Friend WithEvents GridView3 As DevExpress.XtraGrid.Views.Grid.GridView
        Private lst As ArrayList
        Public Sub setNotShowPopUpEnter()
            Me.flagEnter = True
        End Sub
        'Public Sub New()
        '    MyBase.SetStyle(ControlStyles.UserPaint, True)
        '    MyBase.UpdateStyles()
        '    Me.EnterMoveNextControl = True
        '    MyBase.Properties.TextEditStyle = TextEditStyles.DisableTextEditor
        '    MyBase.Properties.ImmediatePopup = True
        '    MyBase.Properties.AllowNullInput = DefaultBoolean.True
        '    Me.Properties.AllowDropDownWhenReadOnly = DefaultBoolean.False
        '    Me.BorderStyle = BorderStyles.NoBorder
        '    Me.InitializeComponent()
        'End Sub

        Private Sub view_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles view.CustomDrawRowIndicator
            Dim view As GridView = DirectCast(sender, GridView)
            Dim indicatorIcon As Boolean = False
            If e.RowHandle >= 0 Then 'trừ trường hợp -1
                'Check whether the indicator cell belongs to a data row
                If e.Info.IsRowIndicator Then
                    e.Info.DisplayText = (e.RowHandle + 1).ToString()
                    If Not indicatorIcon Then
                        e.Info.ImageIndex = -1
                    End If
                End If
            End If
        End Sub
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            If (disposing AndAlso (Not Me.components Is Nothing)) Then
                Me.components.Dispose()
            End If
            MyBase.Dispose(disposing)
        End Sub


        Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
            MyBase.OnPaint(pe)
            ControlPaint.DrawBorder(pe.Graphics, pe.ClipRectangle, MyBase.Parent.BackColor, 0, ButtonBorderStyle.Solid, MyBase.Parent.BackColor, 0, ButtonBorderStyle.Solid, MyBase.Parent.BackColor, 0, ButtonBorderStyle.Solid, Color.Black, 1, ButtonBorderStyle.Dotted)
            'If Me.Properties.ReadOnly.Equals(True) AndAlso (Not flagPaint) Then
            '    Me.flagPaint = True
            '    Me.Properties.Buttons(0).Visible = False
            '    Me.flagPaint = False
            'End If
            'If Me.Properties.ReadOnly.Equals(True) = False AndAlso (Not flagPaint) Then
            '    Me.flagPaint = True
            '    Me.Properties.Buttons(0).Visible = True
            '    Me.flagPaint = False
            'End If

        End Sub
        'Friend WithEvents fProperties As DevExpress.XtraEditors.Repository.RepositoryItemSearchLookUpEdit
        'Friend WithEvents fPropertiesView As DevExpress.XtraGrid.Views.Grid.GridView
        'Friend WithEvents GridView1 As DevExpress.XtraGrid.Views.Grid.GridView
        'Friend WithEvents GridView2 As DevExpress.XtraGrid.Views.Grid.GridView
        'Friend WithEvents GridView3 As DevExpress.XtraGrid.Views.Grid.GridView
        'Friend WithEvents GridView4 As DevExpress.XtraGrid.Views.Grid.GridView


        ' Fields
        Private components As IContainer

        Private Sub ISearchLookupEdit_EditValueChanging(sender As Object, e As ChangingEventArgs)

        End Sub
        'Private Sub ISearchLookupEdit_GotFocus(sender As Object, e As System.EventArgs) Handles Me.GotFocus
        '    If (IsDBNull(Me.EditValue) Or IsNothing(Me.EditValue)) And Me.IsPopupOpen = False And Me.CanShowPopup = True Then
        '        'SendKeys.Send("%{DOWN}")
        '        Me.ShowPopup()
        '    End If
        'End Sub


        Private Sub fPropertiesView_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs)
            Dim view As GridView = DirectCast(sender, GridView)
            Dim indicatorIcon As Boolean = False
            If e.RowHandle >= 0 Then 'trừ trường hợp -1
                'Check whether the indicator cell belongs to a data row
                If e.Info.IsRowIndicator Then
                    e.Info.DisplayText = (e.RowHandle + 1).ToString()
                    If Not indicatorIcon Then
                        e.Info.ImageIndex = -1
                    End If
                End If
            End If
        End Sub

        Private Sub GridView5_CustomDrawRowIndicator(sender As Object, e As RowIndicatorCustomDrawEventArgs) Handles GridView5.CustomDrawRowIndicator
            Dim view As GridView = DirectCast(sender, GridView)
            Dim indicatorIcon As Boolean = False
            If e.RowHandle >= 0 Then 'trừ trường hợp -1
                'Check whether the indicator cell belongs to a data row
                If e.Info.IsRowIndicator Then
                    e.Info.DisplayText = (e.RowHandle + 1).ToString()
                    If Not indicatorIcon Then
                        e.Info.ImageIndex = -1
                    End If
                End If
            End If
        End Sub
        Public Function getRows() As DataRow
            Return rows
        End Function
        Public Custom Event UpdateDisplayFilter As UpdateDisplayFilterHandler
            AddHandler(ByVal value As UpdateDisplayFilterHandler)
                AddHandler Me.Properties.UpdateDisplayFilter, value
            End AddHandler
            RemoveHandler(ByVal value As UpdateDisplayFilterHandler)
                RemoveHandler Me.Properties.UpdateDisplayFilter, value
            End RemoveHandler
            RaiseEvent(ByVal sender As Object, ByVal e As DisplayFilterEventArgs)
            End RaiseEvent
        End Event
        <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
        Public Shadows ReadOnly Property Properties() As RepositoryItemISearchLookUpEdit
            Get
                Return TryCast(MyBase.Properties, RepositoryItemISearchLookUpEdit)
            End Get
        End Property
        Shared Sub New()
            RepositoryItemISearchLookUpEdit.RegisterCustomEdit()
        End Sub
        Protected Overrides Function CreatePopupForm() As DevExpress.XtraEditors.Popup.PopupBaseForm
            Return New CustomPopupSearchLookUpEditForm(Me)
        End Function
        Public Sub New()
            MyBase.SetStyle(ControlStyles.UserPaint, True)
            MyBase.UpdateStyles()
            Me.EnterMoveNextControl = True
            MyBase.Properties.AllowNullInput = DefaultBoolean.True
            Me.BorderStyle = BorderStyles.NoBorder
        End Sub
        Public Overrides ReadOnly Property EditorTypeName() As String
            Get
                Return RepositoryItemISearchLookUpEdit.CustomEditName
            End Get
        End Property

        Private Sub ISearchLookupEdit_EditValueChanging1(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles Me.EditValueChanging
            Dim editor As SearchLookUpEdit = CType(sender, SearchLookUpEdit)
            Dim rowhandle As Integer = Me.Properties.GetIndexByKeyValue(e.NewValue)
            'MessageBox.Show(editor.Properties.View.GetRowCellValue(rowhandle, "ma"))
            'MessageBox.Show(editor.Properties.View.GetDataRow(rowhandle).Item(0).ToString())
            Me.rows = editor.Properties.View.GetDataRow(rowhandle)
        End Sub

        Private Sub ISearchLookupEdit_Enter1(sender As Object, e As System.EventArgs) Handles Me.Enter
            If (IsDBNull(Me.EditValue) Or IsNothing(Me.EditValue)) And Me.IsPopupOpen = False And Me.CanShowPopup = True And Me.flagEnter = False Then
                BeginInvoke(New MethodInvoker(AddressOf ShowPopup))
            End If
        End Sub
        Private Sub OnUpdateDisplayFilter(ByVal sender As Object, ByVal e As DisplayFilterEventArgs) Handles Me.UpdateDisplayFilter
            If e.FilterText <> String.Empty Then
                e.FilterText = """"c + e.FilterText & """"c
            End If
        End Sub
        Public Class CustomPopupSearchLookUpEditForm
            Inherits PopupSearchLookUpEditForm
            Public Sub New(edit As SearchLookUpEdit)
                MyBase.New(edit)
            End Sub
            Protected Overrides Sub UpdateDisplayFilter(displayFilter As String)
                MyBase.UpdateDisplayFilter(""""c & displayFilter & """"c)
            End Sub
        End Class
    End Class
    Public Class CustomPopupSearchLookUpEditForm
        Inherits PopupSearchLookUpEditForm
        Public Sub New(ByVal edit As SearchLookUpEdit)
            MyBase.New(edit)
        End Sub

        Protected Overrides Sub UpdateDisplayFilter(ByVal displayFilter As String)
            Dim args As New DisplayFilterEventArgs(displayFilter)
            Properties.RaiseUpdateDisplayFilter(args)
            MyBase.UpdateDisplayFilter(args.FilterText)
        End Sub

        Public Shadows ReadOnly Property Properties() As RepositoryItemISearchLookUpEdit
            Get
                Return TryCast(MyBase.Properties, RepositoryItemISearchLookUpEdit)
            End Get
        End Property
    End Class
End Namespace

